package com.villaday.idf.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.villaday.idf.AppContext;
import com.villaday.idf.base.greendao.CityDao;
import com.villaday.idf.base.greendao.DaoMaster;
import com.villaday.idf.base.greendao.DaoSession;
import com.villaday.idf.model.City;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by tudou on 2018/10/17.
 */

public class DbCityUtils {

    private static DbCityUtils db;
    private final static String dbName = "city_db";
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;


    public DbCityUtils() {
        context = AppContext.getInstance();
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }


  /*  *
     * 获取单例
     *
     * @return*/

    public static DbCityUtils getInstance() {
        if (db == null) {
            synchronized (DbCityUtils.class) {
                if (db == null) {
                    db = new DbCityUtils();
                }
            }
        }
        return db;
    }


   /* *
     * 获取可读数据库*/

    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

  /*  *
     * 获取可写数据库*/

    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }


    public void save(City info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        downInfoDao.insert(info);
    }

    public void update(City info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        downInfoDao.update(info);
    }

    public void deleteAll() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        downInfoDao.deleteAll();
    }



    public List<City> queryDownAll() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        QueryBuilder<City> qb = downInfoDao.queryBuilder();
        return qb.list();
    }

    public City queryByName(String something) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        City mingEq = downInfoDao.queryBuilder().where(CityDao.Properties.Name.eq(something)).unique();

        return mingEq;
    }

    public List<City> queryDownAllOrderByDate() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        QueryBuilder<City> qb = downInfoDao.queryBuilder()
                .orderDesc(CityDao.Properties.Date);
        return qb.list();
    }

    public void insertOrReplace(String code, String citrId, String name, long serverTime, String shortCode, boolean isCheck, long date) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CityDao downInfoDao = daoSession.getCityDao();
        downInfoDao.insertOrReplace(new City(code,citrId,name,serverTime,shortCode,isCheck,date));


    }
}
